#pragma once
#include<iostream>
#include<vector>
using namespace std;

namespace wzy
{
	template<class T>
	struct HashNode
	{
		HashNode<T>* _next=nullptr;
		T _data;

		HashNode(const T& data)
			:_data(data)
			, _next(nullptr)
		{}

	};

	template<class K>
	struct Setof
	{
		size_t operator()(const K& key)
		{
			return (size_t)key;
		}
	};

	template<>
	struct Setof<string>
	{
		size_t operator()( const string& key)
		{
			size_t sum = 0;
			for (auto ch : key)
			{
				sum *= 31;
				sum += ch;
			}
			return sum;
		}
	};

	template<class K,class T,class getkey,class Set>
	class HashTable;

	template<class K,class T,class Ref,class Ptr,class getkey,class Set>
	struct _Iterator
	{
		typedef HashNode<T> Node;
		typedef _Iterator<K, T, Ref,Ptr,getkey, Set> Self;


		const HashTable<K, T, getkey, Set>* _pht;
		Node* _node;
		size_t _hashi;

		_Iterator(HashTable<K, T, getkey, Set>* pht, Node* node, size_t hashi)
			:_pht(pht)
			,_node(node)
			,_hashi(hashi)
		{}

		_Iterator(const HashTable<K,T,getkey,Set>* pht,Node* node,size_t hashi)
			:_pht(pht)
			,_node(node)
			,_hashi(hashi)
		{}

		Ref operator*()
		{
			return  _node->_data;
		}

		Self& operator++()
		{
			if (_node->_next == nullptr)
			{
				size_t i;
				for ( i = _hashi+1; i < _pht->_tables.size(); i++)
				{
					Node* cur = _pht->_tables[i];
					if (cur)
					{
						_node = cur;
						_hashi = i;
						break;
					}
				}

				if (i == _pht->_tables.size())
				{
					_node = nullptr;
				}
			}
			else
			{
				_node = _node->_next;
			}
			return *this;
		}

		Ptr operator->()
		{
			return &_node->_data;
		}

		bool operator !=(const Self& s)
		{
			return _node != s._node;
		}
	};

	template<class K, class T, class getkey, class Set>
	class HashTable
	{
		typedef HashNode<T> Node;
		//typedef HashTable<K, T, getkey, Set> Hash;


		template<class K,class T,class Ref,class Ptr,class getkey,class Set>
		friend struct _Iterator;


	public:

		typedef _Iterator<K, T, T&, T*, getkey, Set> iterator;
		typedef _Iterator<K, T, const T&, const T*, getkey, Set> const_iterator;
		typedef HashTable<K, T, getkey, Set> Hash;

		HashTable()
		{
			_tables.resize(10);
		}

		~HashTable()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				Node* cur = _tables[i];
				while (cur)
				{
					Node* temp = cur->_next;
					delete cur;
					cur = temp;
				}
				_tables[i] = nullptr;
			}
		}

		iterator begin()
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				if (_tables[i])
				{
					return  iterator(this, _tables[i], i);
				}
			}
			return end();
		}

		iterator end() 
		{
			return  iterator(this, nullptr, -1);
		}

		const_iterator begin() const
		{
			for (size_t i = 0; i < _tables.size(); i++)
			{
				if (_tables[i])
				{
					return  const_iterator(this, _tables[i], i);
				}
			}
			return end();
		}

		const_iterator end() const
		{
			return const_iterator(this, nullptr, -1);
		}

		bool Insert(const T& data)
		{

			getkey get;
			Set Setof;

			if (Find(get(data)))
			{
				return false;
			}

			if ((_n * 10) / _tables.size() >= 7)
			{
				Hash newhash;
				newhash._tables.resize(_tables.size() * 2);
				for (size_t i = 0; i < _tables.size(); i++)
				{
					Node* cur = _tables[i];
					while (cur)
					{
						size_t hashi = Setof(get(cur->_data)) % newhash._tables.size();
						Node* next1 = cur->_next;
						Node* next2 = newhash._tables[hashi];
						newhash._tables[hashi] = cur;
						newhash._tables[hashi]->_next = next2;
						cur = next1;
					}

					_tables[i] = nullptr;
				}

				_tables.swap(newhash._tables);

			}

			size_t hashi = Setof(get(data))%_tables.size();
			Node* temp = _tables[hashi];
			Node* newnode = new Node(data);
			_tables[hashi] = newnode;
			newnode->_next = temp;
			_n++;

			return true;
		}


		Node* Find(const K& key)
		{
			getkey get;
			Set Setof;
			size_t hashi = Setof(key)%_tables.size();
			Node* cur = _tables[hashi];
			while (cur)
			{
				if (get(cur->_data) == key)
				{
					return cur;
				}
				cur = cur->_next;
			}
			return nullptr;
		}

		bool Erase(const K& key)
		{
			Set set;
			getkey get;
			size_t hashi = set(key)%_tables.size();
			Node* cur = _tables[hashi];
			Node* prev = nullptr;
			while (cur)
			{
				if (get(cur->_data) == key)
				{
					if (prev == nullptr)
					{
						Node* temp = cur->_next;
						delete cur;
						_tables[hashi] = temp;
					}
					else
					{
						prev = cur->_next;
						delete cur;
					}
					_n--;
					return true;

				}
				prev = cur;
				cur = cur->_next;
			}
			return false;
		}


	public:
		vector<Node*> _tables;
		size_t _n = 0;
	};
}